package wili.insertCode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.function.BiFunction;
import java.util.regex.Pattern;

import wili.c.File9a;
import wili.c.Misc9a;

/**
 * @author weila 2022年6月23日
 */
public class Coms2 {
	public static final String PreMark = "//patched by InsertTimeCostMethod_main";

	public static final String newNameSuffix = "_weilai345";

	/**
	 * CopiedFilesList saved here: with type of List<File[]>.<br>
	 */
	public static File file_copiedFilesList; //

	/**
	 * backupFile=srcBackupRoot/relativePath(srcRootBase, srcFile)
	 */
	public static File srcBackupRoot;

	/**
	 * So can get class of java file.
	 */
	public static File[] srcRoots;

	public static BiFunction<File, File, File[]> getClassFromJava;

	public static File[] filesOf(String... paths) {
		File[] ret = new File[paths.length];
		for (int i = 0; i < ret.length; i++) {
			String st = paths[i].trim();
			//Coms.log(st);
			ret[i] = new File(st);
		}
		return ret;
	}

	public static void log(Object msg) {
		log(1, msg);
	}

	public static void err(Object msg) {
		System.err.println(msg);
	}

	public static final boolean PRINT2aaa = "".isEmpty();

	public static void log(int opt, Object... msg) {
		if (!PRINT2aaa)
			return;
		if (opt < 0)
			return;
		String ret;
		ret = Misc9a.concat(msg, "");
		if (ret.contains("cname="))
			Coms2.pause();
		System.out.println(ret);
	}

	public static void asert(boolean is) {
		asert(is, "");
	}

	public static void asert(boolean is, Object msg) {
		if (!is)
			throw new RuntimeException(msg.toString());
	}

	final static String yyyyMMdd_HHmmss = "yyyy-MM-dd HH:mm:ss";

	public static String dateTimeStrOfNow() {
		SimpleDateFormat df = new SimpleDateFormat(yyyyMMdd_HHmmss, Locale.CHINA);
		String ret = df.format(new Date());
		return ret;
	}

	public static List<Pattern> patternsOfRegexps(List<String> regExps) {
		List<Pattern> ret = new ArrayList<>(regExps.size());
		for (String reg : regExps) {
			ret.add(Pattern.compile(reg));
		}
		return ret;
	}

	/**
	 * @return true, only if match anyone or not match anyone.
	 */
	public static boolean matchRegExps(String st, boolean match, List<Pattern> regExps) {
		if (match) {
			for (Pattern pa : regExps) {
				if (pa.matcher(st).matches())
					return true;
			}
			return false;
		} else {
			for (Pattern pa : regExps) {
				if (pa.matcher(st).matches())
					return false;
			}
			return true;
		}
	}

	public static File getJavaFileOfBackupFile(File bfile) {
		//        //may be has many source file dir. so can't get source file based on one sourceRoot.
		//        return jbFileMap.get(bfile);
		//String rPath = bfile.getAbsolutePath();
		String rPath = File9a.relativePathOf(Coms2.srcBackupRoot, bfile);
		Coms2.asert(rPath.startsWith("\\"), rPath);
		File jfile = new File("c:", rPath);
		return jfile;

	}

	public static File getBackupFile(File jfile) {
		//String path = File9.relativePathOf(Coms.srcRootBase, file);
		String rPath = jfile.getAbsolutePath();
		rPath = rPath.substring(rPath.indexOf(':') + 1);
		File backfile = new File(Coms2.srcBackupRoot, rPath);
		return backfile;
	}

	public static void pause() {
	}

	/**
	 * e.g. isInt(a / b), 则a,b可整除.
	 */
	public static boolean isInt(double va) {
		return isInt(va, 1e-10);
	}

	public static long round(Number num) {
		if (num instanceof Long) {
			return Math.round(num.doubleValue());//if input long, will return int
		} else if (num instanceof Integer) {
			return Math.round(num.intValue());
		}
		return Math.round(num.doubleValue());
	}

	/**
	 * e.g. isInt(a / b), 则a,b可整除.
	 */
	public static boolean isInt(double va, double precise) {
		//warn: if precise=1e10, 0.00000000001a will be 0
		if (Double.isNaN(va))
			return false;
		va = Math.abs(va);
		if (va >= 1) {
			precise = 1e-8;//非纯小数，对小数不敏感。暂未发现问题
			//eg.2.0000000004
		} else {
			//eg.0.0000000004
		}
		if (va <= precise) //值为0？
			return true;
		if (Math.abs(round(va) - va) < precise)
			return true;// 1e-6;
		return false;
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> emptyList() {
		return Collections.EMPTY_LIST;
	}

}
